/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Ad_XuatChieuInternalFrame.java
 *
 * Created on 04-04-2012, 22:43:22
 */
package GUI;

import DAO.MySQLDataAccessHelper;
import DAO.PhimDAO;
import DAO.PhongChieuDAO;
import DAO.XuatChieuDAO;
import POJO.XuatChieuPoJo;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author tran quoc thinh
 */
public class Ad_XuatChieuInternalFrame extends javax.swing.JInternalFrame {
      private String MaPhim="";
      private String MaPhong="";
      private String MaXC=null;
      private int flag=0;
      private int a;
    /** Creates new form Ad_XuatChieuInternalFrame */
    public Ad_XuatChieuInternalFrame() {
        initComponents();
    }
    
     private void AddElementPhong() throws SQLException{
        ResultSet rs=PhongChieuDAO.LayBangPhongChieu();
        Vector vt=new Vector();
        while(rs.next()){            
            vt.add(rs.getString("TenPhong"));
        }
        DefaultComboBoxModel model =new DefaultComboBoxModel();
        model.addElement("Chon Phong:");
        for(int i=0;i<vt.size();i++){
            model.addElement(vt.get(i));
        }
        this.jcbPhongChieu.setModel(model);
        MySQLDataAccessHelper helper =new MySQLDataAccessHelper();
        helper.Close();
     }
       private void AddElementPhim() throws SQLException{
        ResultSet rs=PhimDAO.DSPhim();
        Vector vt=new Vector();
        while(rs.next()){            
            vt.add(rs.getString("TenPhim"));
        }
        DefaultComboBoxModel model =new DefaultComboBoxModel();
        model.addElement("Chon Phim:");
        for(int i=0;i<vt.size();i++){
            model.addElement(vt.get(i));
        }
        this.jcbTenPhim.setModel(model);
        MySQLDataAccessHelper helper =new MySQLDataAccessHelper();
        helper.Close();
     }    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jcbPhongChieu = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jtxtNgayChieu = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jtxtGioChieu = new javax.swing.JTextField();
        jbtnCapNhat = new javax.swing.JButton();
        jbtnThem = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jcbTenPhim = new javax.swing.JComboBox();
        jbtnXoa = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Quản Lý Xuất Chiếu");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jLabel1.setText("Chọn Phòng Chiếu");

        jcbPhongChieu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbPhongChieuActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jtxtNgayChieu.setText("dd/mm/yy");

        jLabel3.setText("Ngày Chiếu");

        jLabel4.setText("Giờ Chiếu");

        jtxtGioChieu.setText("hh:mm");

        jbtnCapNhat.setText("Cập Nhật");
        jbtnCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCapNhatActionPerformed(evt);
            }
        });

        jbtnThem.setText("Thêm");
        jbtnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnThemActionPerformed(evt);
            }
        });

        jLabel2.setText("Tên Phim");

        jcbTenPhim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbTenPhimActionPerformed(evt);
            }
        });

        jbtnXoa.setText("Xóa");
        jbtnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnXoaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(92, 92, 92)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jtxtNgayChieu, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                                    .addComponent(jtxtGioChieu, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                                    .addComponent(jcbTenPhim, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(89, 89, 89))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(100, 100, 100)
                                .addComponent(jbtnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jbtnXoa, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
                                .addGap(18, 18, 18)
                                .addComponent(jbtnCapNhat)
                                .addGap(99, 99, 99))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jcbPhongChieu, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jcbPhongChieu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jtxtNgayChieu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jtxtGioChieu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jcbTenPhim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(255, 255, 255)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jbtnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jbtnCapNhat, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                            .addComponent(jbtnXoa, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 377, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
 
    private void jcbPhongChieuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbPhongChieuActionPerformed
        // TODO add your handling code here:
        ResultSet rs=null;
        ResultSet rs1=null;
        rs=PhongChieuDAO.LayBangPhongChieu();
        String tenphong=(String)this.jcbPhongChieu.getSelectedItem();
        if(tenphong.equals("Chon Phong:")){
            JOptionPane.showMessageDialog(null,"Thong Bao","Chưa Chọn",JOptionPane.INFORMATION_MESSAGE);
            flag=1;
        }else{
           
            try {
                while (rs.next()) {
                    if(rs.getString("TenPhong").contentEquals(tenphong)){
                        MaPhong=rs.getString("MaPhong");
                    }
                }
            } catch (SQLException ex) {
                Logger.getLogger(Ad_XuatChieuInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        try{  
           rs=null;           
           rs=XuatChieuDAO.LayBangXuatChieuTheoMaPhong(MaPhong);
           rs1=PhimDAO.DSPhim();
           DefaultTableModel model =new DefaultTableModel();
           model.addColumn("MaXC");
           model.addColumn("Gio Chieu");
           model.addColumn("Ngay Chieu");
           model.addColumn("Ten Phim");
           String tenPhim="";
           if(rs!=null){
               while(rs.next()){   
                   while(rs1.next()){
                       if(rs.getString("MaPhim").equals(rs1.getString("MaPhim"))){
                           tenPhim=rs1.getString("TenPhim");
                       }
                   }
                   rs1.first();
                   Object[] a={rs.getInt("MaXC"),rs.getString("GioChieu"),rs.getString("NgayChieu"),tenPhim};               
                   model.addRow(a);
               }
               this.jTable1.setModel(model); 
           }                 
        }catch(Exception ex){
            System.err.println(ex);
        }       
        MySQLDataAccessHelper helper=new MySQLDataAccessHelper();
        helper.Close();
                     
    }//GEN-LAST:event_jcbPhongChieuActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        try {
            // TODO add your handling code here:
            AddElementPhong();
            AddElementPhim();
            //AddElementPhim(); 
            this.jTable1.validate();
        } catch (SQLException ex) {
            Logger.getLogger(Ad_XuatChieuInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_formInternalFrameOpened

    private void jbtnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnThemActionPerformed
        // TODO add your handling code here:
        if(flag==1){
             JOptionPane.showMessageDialog(null,"Thong Bao","Chưa chọn Phim",JOptionPane.INFORMATION_MESSAGE);
        }if(flag==2){
            JOptionPane.showMessageDialog(null,"Thong Bao","Chưa Chọn Phòng",JOptionPane.INFORMATION_MESSAGE);
        }else{
            XuatChieuPoJo xc=new XuatChieuPoJo();
            xc.setMaPhim(MaPhim);
            xc.setMaPhong(MaPhong);
            Date ngaychieu = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");   
            try {
                ngaychieu= sdf.parse(this.jtxtNgayChieu.getText().trim().toString());
            } catch (ParseException ex) {
                Logger.getLogger(Ad_XuatChieuInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
            }       
            
           // java.util.Date myDate = new java.util.Date(ngaychieu);
            java.sql.Date sqlDate = new java.sql.Date(ngaychieu.getTime());
            xc.setNgayChieu(sqlDate);
            String gio=this.jtxtGioChieu.getText();
            String [] chuoi=gio.split(":");
            Time giochieu=new Time(Integer.parseInt(chuoi[0]),Integer.parseInt(chuoi[1]),0);
            xc.setGioChieu(giochieu);
            XuatChieuDAO.ThemXC(xc);
            jcbPhongChieuActionPerformed(evt);
        }      
        
        
    }//GEN-LAST:event_jbtnThemActionPerformed

    private void jcbTenPhimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbTenPhimActionPerformed
        // TODO add your handling code here:
        ResultSet rs=null;       
        rs=PhimDAO.DSPhim();
        String tenphim=(String)this.jcbTenPhim.getSelectedItem();
        if(tenphim.equals("Chon Phim:")){
            flag=2;
            JOptionPane.showMessageDialog(null,"Thong Bao","Chưa Chọn",JOptionPane.INFORMATION_MESSAGE);
        }else{
           
            try {
                while (rs.next()) {
                    if(rs.getString("TenPhim").contentEquals(tenphim)){
                        MaPhim=rs.getString("MaPhim");
                    }
                }
            } catch (SQLException ex) {
                Logger.getLogger(Ad_XuatChieuInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }    
      
        MySQLDataAccessHelper helper=new MySQLDataAccessHelper();
        helper.Close();
    }//GEN-LAST:event_jcbTenPhimActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
         a=this.jTable1.getSelectedRow();
         this.jtxtGioChieu.setText(this.jTable1.getValueAt(a, 1).toString());
         this.jtxtNgayChieu.setText(this.jTable1.getValueAt(a, 2).toString());
         this.jcbTenPhim.setSelectedItem(this.jTable1.getValueAt(a, 3).toString());
    
    }//GEN-LAST:event_jTable1MouseClicked

    private void jbtnCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCapNhatActionPerformed
        // TODO add your handling code here:
            XuatChieuPoJo xc=new XuatChieuPoJo();
            xc.setMaPhim(MaPhim);
            xc.setMaPhong(MaPhong);
            Date ngaychieu = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");   
            try {
                String nc=this.jtxtNgayChieu.getText().trim().toString();
                nc=nc.replace("-", "/");
                ngaychieu= sdf.parse(nc);
                
            } catch (ParseException ex) {
                Logger.getLogger(Ad_XuatChieuInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
            }       
            
           // java.util.Date myDate = new java.util.Date(ngaychieu);
            java.sql.Date sqlDate = new java.sql.Date(ngaychieu.getTime());
            xc.setNgayChieu(sqlDate);
            String gio=this.jtxtGioChieu.getText();
            String [] chuoi=gio.split(":");
            Time giochieu=new Time(Integer.parseInt(chuoi[0]),Integer.parseInt(chuoi[1]),0);
            xc.setGioChieu(giochieu);
          
            ResultSet rs=XuatChieuDAO.LayBangXuatChieu();      
            String gc=this.jTable1.getValueAt(a, 1).toString();
            String nc=this.jTable1.getValueAt(a, 2).toString();
            int ma=0;
        try {
            while(rs.next()){              
                if(nc.equals(rs.getString("NgayChieu")) && gc.equals(rs.getString("GioChieu"))){
                    ma=rs.getInt("MaXC");
                }            
            }
        } catch (SQLException ex) {
            Logger.getLogger(Ad_XuatChieuInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
            xc.setMaXC(ma);
            XuatChieuDAO.SuaPhim(xc);
            jcbPhongChieuActionPerformed(evt);
    }//GEN-LAST:event_jbtnCapNhatActionPerformed

    private void jbtnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnXoaActionPerformed
        // TODO add your handling code here:
          if(a==-1){
              JOptionPane.showMessageDialog(null,"Thông Báo","Chưa Chọn",JOptionPane.INFORMATION_MESSAGE);
         }else{
              int maxc=Integer.parseInt(this.jTable1.getValueAt(a, 0).toString());
              int n=XuatChieuDAO.XoaXuatChieu(maxc);
             if (n == 1) {
                 JOptionPane.showMessageDialog(null, "Xóa Xuat Chiếu", "Thanh Cong", JOptionPane.INFORMATION_MESSAGE);
                 
             } else {
                 JOptionPane.showMessageDialog(null, "Xóa Xuất Chiếu", "Khong Thanh Cong", JOptionPane.INFORMATION_MESSAGE);
             }
              jcbPhongChieuActionPerformed(evt);
              
         }
    }//GEN-LAST:event_jbtnXoaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton jbtnCapNhat;
    private javax.swing.JButton jbtnThem;
    private javax.swing.JButton jbtnXoa;
    private javax.swing.JComboBox jcbPhongChieu;
    private javax.swing.JComboBox jcbTenPhim;
    private javax.swing.JTextField jtxtGioChieu;
    private javax.swing.JTextField jtxtNgayChieu;
    // End of variables declaration//GEN-END:variables
}
